package at.guigu.service;

import at.guigu.dao.BrandDao;
import at.guigu.pojo.Brand;
import at.guigu.utils.SqlSessionFactoryUtil;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import java.io.IOException;
import java.util.List;
import java.util.Map;

public class BrandService {
    /*1 获取SqlSessionFactory对象
    * 由于会多次用到SqlSessionFactory对象，所以将其放在成员变量位置，以供所有该类下方法使用
    * */
    SqlSessionFactory sqlSessionFactory = SqlSessionFactoryUtil.getSqlSessionFactory();

    //查询所有条数据
    public List<Brand> getAll() throws IOException {

        //2 获取SqlSession对象，执行SQL语句
        //2.1 通过SqlSessionFactory对象获取SqlSession对象
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //2.2 通过SqlSession对象获取Dao接口UserDao的代理对象
        BrandDao brandDao = sqlSession.getMapper(BrandDao.class);
        //2.3 执行sql语句
        List<Brand> brands = brandDao.All();
        // 释放资源
        sqlSession.close();
        return brands;
    }
    //查询单条数据：通过id查询
    public Brand getById(int id) throws IOException {
        //2 获取SqlSession对象，执行SQL语句
        //2.1 通过SqlSessionFactory对象获取SqlSession对象
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //2.2 通过SqlSession对象获取Dao接口UserDao的代理对象
        BrandDao brandDao = sqlSession.getMapper(BrandDao.class);
        //2.3 执行sql语句
        Brand brand = brandDao.selectById(id);
        // 释放资源
        sqlSession.close();
        return brand;
    }

    //静态单条件查询
    public List<Brand> selectBySingleConOne(int id) throws IOException {
        //2 获取SqlSession对象，执行SQL语句
        //2.1 通过SqlSessionFactory对象获取SqlSession对象
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //2.2 通过SqlSession对象获取Dao接口UserDao的代理对象
        BrandDao brandDao = sqlSession.getMapper(BrandDao.class);
        //2.3 执行sql语句
        List<Brand> brands = brandDao.selectBySingleConOne(id);
        // 释放资源
        sqlSession.close();
        return brands;
    }
/*---------------------------------------------------------------------------------------*/
    //动态单条件查询——对象参数接收
    public List<Brand> selectBySingleConTwo(Brand brand) {
        //2 获取SqlSession对象，执行SQL语句
        //2.1 通过SqlSessionFactory对象获取SqlSession对象
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //2.2 通过SqlSession对象获取Dao接口UserDao的代理对象
        BrandDao brandDao = sqlSession.getMapper(BrandDao.class);
        //2.3 执行sql语句
        List<Brand> brands = brandDao.selectBySingleConTwo(brand);
        sqlSession.close();
        return brands;
    }
    //动态单条件查询——Map集合参数接收
    public List<Brand> selectBySingleConTwoo(Map map) {
        //2 获取SqlSession对象，执行SQL语句
        //2.1 通过SqlSessionFactory对象获取SqlSession对象
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //2.2 通过SqlSession对象获取Dao接口UserDao的代理对象
        BrandDao brandDao = sqlSession.getMapper(BrandDao.class);
        //2.3 执行sql语句
        List<Brand> brands = brandDao.selectBySingleConTwoo(map);
        sqlSession.close();
        return brands;
    }
/*---------------------------------------------------------------------------------------*/
    //动态多条件查询——对象参数接收
    public List<Brand> selectByMaxConOne(Brand brand){

        //4 获取SqlSession对象，执行SQL语句
        //4.1 通过SqlSessionFactory对象获取SqlSession对象
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //4.2 通过SqlSession对象获取Dao接口UserDao的代理对象
        BrandDao brandDao = sqlSession.getMapper(BrandDao.class);
        //4.3 执行sql语句
        List<Brand> brands = brandDao.selectByMaxConOne(brand);
        sqlSession.close();
        return brands;
    }
    //动态多条件查询——Map集合参数接收
    public List<Brand> selectByMaxConTwo(Map map){

        //4 获取SqlSession对象，执行SQL语句
        //4.1 通过SqlSessionFactory对象获取SqlSession对象
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //4.2 通过SqlSession对象获取Dao接口UserDao的代理对象
        BrandDao brandDao = sqlSession.getMapper(BrandDao.class);
        //4.3 执行sql语句
        List<Brand> brands = brandDao.selectByMaxConTwo(map);
        sqlSession.close();
        return brands;
    }
/*===================================================================*/
    // 基础添加
    public boolean add(Brand brand) {
        //4 获取SqlSession对象，执行SQL语句
        //4.1 通过SqlSessionFactory对象获取SqlSession对象
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //4.2 通过SqlSession对象获取Dao接口UserDao的代理对象
        BrandDao brandDao = sqlSession.getMapper(BrandDao.class);
        //4.3 执行sql语句
        boolean f = brandDao.add(brand);
        //手动提交事务
        sqlSession.commit();
        sqlSession.close();
        return f;
    }
/*===================================================================*/
    // 修改商品数据：修改动态字段
    public int updatePart(Brand brand) {
        //4 获取SqlSession对象，执行SQL语句
        //4.1 通过SqlSessionFactory对象获取SqlSession对象
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //4.2 通过SqlSession对象获取Dao接口UserDao的代理对象
        BrandDao brandDao = sqlSession.getMapper(BrandDao.class);
        //4.3 执行sql语句,返回受影响的行数
        int nums = brandDao.updatePart(brand);
        //手动提交事务
        sqlSession.commit();
        sqlSession.close();
        return nums;
    }
/*===================================================================*/
    // 单条删除（删除一条数据（删除一个商品））
    public boolean deleteSingle(int id) {
        //4 获取SqlSession对象，执行SQL语句
        //4.1 通过SqlSessionFactory对象获取SqlSession对象
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //4.2 通过SqlSession对象获取Dao接口UserDao的代理对象
        BrandDao brandDao = sqlSession.getMapper(BrandDao.class);
        //4.3 执行sql语句,返回受影响的行数
        boolean f = brandDao.deleteSingle(id);
        //手动提交事务
        sqlSession.commit();
        sqlSession.close();
        return f;
    }
    // 批量删除
    public boolean deleteByIds(int[] ids) {
        //4 获取SqlSession对象，执行SQL语句
        //4.1 通过SqlSessionFactory对象获取SqlSession对象
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //4.2 通过SqlSession对象获取Dao接口UserDao的代理对象
        BrandDao brandDao = sqlSession.getMapper(BrandDao.class);
        //4.3 执行sql语句,返回受影响的行数
        boolean f = brandDao.deleteByIds(ids);
        //手动提交事务
        sqlSession.commit();
        sqlSession.close();
        return f;
    }
}
